
package com.example.demo.practice.monitoring;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;

import com.example.demo.practice.R;
import com.example.demo.practice.dashboardview.AlarmInfo;
import com.example.demo.practice.dashboardview.FacilityIfs;
import com.example.demo.practice.innerclass.NormalExpandableListAdapter;
import com.example.demo.practice.tool.ServerHttp;
import com.example.demo.practice.ui.TitleView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class AlarmHistory extends Activity {
    private TitleView title;
    private ServerHttp serverHttp;
    private ExpandableListView expandableListView;

    private List<String> groupData;
    private List<List<String>> childData;
    private String contains[];
    private int numbers = 0;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            groupData = new ArrayList<String>();
            childData = new LinkedList<List<String>>();

            JSONArray jsonArray = serverHttp.getData();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    String adName = json.getString("name");
                    String eqindex = json.getString("eqindex");
                    String status = json.getString("status");
                    if(status.equals("报警")){
                        if (groupData.contains(adName)) {
                            for (int j = 0; j < groupData.size(); j++) {
                                if (groupData.get(j).equals(adName)) {
                                    int num = 1;
                                    for (int k = 0;k < childData.get(j).size();k++) {
                                        contains = childData.get(j).get(k).split("：");
                                        if (contains[1].equals(eqindex + "        报警次数")){
                                            numbers = k;
                                            num++;
                                            break;
                                        }
                                    }
                                    if (num==1){
                                        childData.get(j).add("编号：" + eqindex + "        报警次数：1");
                                        break;
                                    }else{
                                        int quantity = Integer.valueOf(contains[2]);
                                        quantity++;
                                        childData.get(j).set(numbers,"编号：" + eqindex + "        报警次数：" + quantity);
                                        break;
                                    }
                                }
                            }
                        }else {
                            groupData.add(adName);
                            List<String> list = new ArrayList<String>();
                            list.add("编号：" + eqindex + "        报警次数：1");
                            childData.add(list);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            serverHttp.clear();
            NormalExpandableListAdapter normalExpandableListAdapter = new NormalExpandableListAdapter(groupData, childData);
            expandableListView.setAdapter(normalExpandableListAdapter);
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_history);
        title = findViewById(R.id.title);
        title.setText("报警历史");
        expandableListView = findViewById(R.id.listView);
        serverHttp = new ServerHttp(handler);
        serverHttp.setUrl("getFacilities");

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Intent intent = new Intent(AlarmHistory.this, AlarmInfo.class);
                startActivity(intent);
                return true;
            }
        });
    }
}
